Storytelling with ggplot2:

Using custom functions to sequence visualizations


McCall Pitcher
Data Visualization Specialist
Center for Data and Visualization Sciences
Duke University Libraries

Let’s break this down

Most colleges improved

But some declined, particularly in retention

A handful met the goal

These 3 colleges improved the most across indicators

Custom function arguments


build_chart <- function(fill_var, color_var, annotation_color) {
  
  ...
  
}

Creating fill_var columns

  1. Positive rate
  1. Negative rate
  1. Met the goal
  1. Selected for case study

Creating fill_var columns

mutate(
positive = case_when(
           metric == "CAR"             & rate > 0 ~"a",
           metric == "Retention Rate"  & rate > 0 ~"b",
           metric == "Graduation Rate" & rate > 0 ~"c",
           T ~ "d"),
negative = case_when(
           metric == "CAR"             & rate < 0 ~"a",
           metric == "Retention Rate"  & rate < 0 ~"b",
           metric == "Graduation Rate" & rate < 0 ~"c",
           T ~ "d"),
goal     = case_when(
           metric == "CAR"             & rate > 1 ~"a",
           metric == "Retention Rate"  & rate > 1 ~"b",
           metric == "Graduation Rate" & rate > 1 ~"c",
           T ~ "d"),
case     = case_when(
           metric == "CAR"            & college %in% c("GGG","MMM","FFF")~"a",
           metric == "Retention Rate" & college %in% c("GGG","MMM","FFF")~"b",
           metric == "Graduation Rate"& college %in% c("GGG","MMM","FFF")~"c",
           T~"d")
)

Creating color_var columns

mutate(
 positive_color =ifelse(positive != "d", "a", "b"),
 negative_color =ifelse(negative != "d", "a", "b"),
 goal_color     =ifelse(goal     != "d", "a", "b"),
 case_color     =ifelse(case     != "d", "a", "b")
 )

The custom function

build_chart <- function(
                        fill_var,                          
                        color_var, 
                        annotation_color
                        ) {
  
ggplot(dat, aes(x = rate, y = college) +
     geom_col(aes(fill = {{ fill_var }})) +                
     geom_text(dat |> filter(rate < 0),
        mapping = aes(x = .1, y = college, label = college, 
                      color = {{ color_var }})) +
     geom_text(dat |> filter(rate > 0),
        mapping = aes(x = -.1, y = college, label = college, 
                      color = {{ color_var }})) +
     geom_segment(brackets, 
        mapping = aes(x = x, xend = xend, y = y, yend = yend),
        color = annotation_color) +
     facet_wrap(~metric, scales = 'free_y') +
     scale_fill_manual(values = c("#949e48", "#ea9f2c",    
                                  "#0290c0", "grey90")) +  
     scale_color_manual(values = c("grey40","grey90"))
}

Running the function

build_function(positive, positive_color, "transparent")


Running the function

build_function(negative, negative_color, "transparent")


Running the function

build_function(goal, goal_color, "grey50") +
    geom_vline(aes(xintercept = 1), color = "grey70", linetype = "dotted")


Running the function

build_function(case, case_color, "grey90") +
    geom_vline(aes(xintercept = 1), color = "grey90", linetype = "dotted")


Thank you!

mccall.pitcher@duke.edu



McCall Pitcher
Data Visualization Specialist
Center for Data and Visualization Sciences
Duke University Libraries